# audit::configgui --
#
#	Toplevel window for configuring audit files,
#	audit level etc.
#

package require audit::audit
package require Tk

namespace eval audit {}

proc audit::showConfigWindow {{customcode ""}} {
    if {[winfo exists .auditconfigwindow]} {
	raise .auditconfigwindow
	return
    }
    toplevel .auditconfigwindow
    wm title .auditconfigwindow [msgcat::mc "Audit configuration window"]

    set p [frame .auditconfigwindow.main]
    grid .auditconfigwindow.main -row 1 -column 1 -sticky news
    grid columnconfigure .auditconfigwindow 1 -weight 1
    grid rowconfigure .auditconfigwindow 1 -weight 1

    eval $customcode

    wm withdraw .auditconfigwindow
    wm resizable .auditconfigwindow 1 0

    label $p.label1 -text [msgcat::mc "Audit level"] -anchor sw
    label $p.label2 -text [msgcat::mc "Audit log file"] -anchor sw
    label $p.label3 -text [msgcat::mc "Audit dump file"] -anchor sw
    label $p.label4 -text [msgcat::mc "Audit limit"] -anchor sw

    label $p.levelLabel -textvariable ::audit::configWindow(levelstring) -anchor nw

    scale $p.levelScale -variable :audit::configWindow(level) -command ::audit::showConfigWindowLevelSet \
	-orient horizontal -from 0 -to 9 -showvalue 0

    entry $p.logfileEntry -textvariable ::audit::configWindow(logfile) -width 40
    entry $p.datafileEntry -textvariable ::audit::configWindow(datafile) -width 40
    entry $p.limitEntry -textvariable ::audit::configWindow(limit) -validatecommand {string is digit %P} -validate key -width 40

    button $p.logfilePop -text "..." -borderwidth 1 -pady 1 -command [list audit::showConfigWindowPopfile ::audit::configWindow(logfile)]
    button $p.datafilePop -text "..." -borderwidth 1 -pady 1 -command [list audit::showConfigWindowPopfile ::audit::configWindow(datafile)]

    frame $p.toolbar

    button $p.toolbar.apply -text [msgcat::mc "Apply"] -command {::audit::showConfigWindowConfigure; destroy .auditconfigwindow}
    button $p.toolbar.cancel -text [msgcat::mc "Cancel"] -command {destroy .auditconfigwindow}

    showConfigWindowCget

    grid $p.label1 - -sticky ew
    grid $p.levelScale - -sticky ew
    grid $p.levelLabel - -sticky ew

    grid $p.label2 - -sticky ew
    grid $p.logfileEntry $p.logfilePop -sticky ew

    grid $p.label3 - -sticky ew
    grid $p.datafileEntry $p.datafilePop -sticky ew

    grid $p.label4 - -sticky ew
    grid $p.limitEntry - -sticky ew

    grid $p.toolbar -

    pack $p.toolbar.apply -side left -fill both
    pack $p.toolbar.cancel -side left -fill both

    grid columnconfigure $p 0 -weight 1

    after idle [list audit::showConfigWindowCenter]
}

proc audit::showConfigWindowCenter {} {
    if {![winfo exists .auditconfigwindow]} {
	return
    }

    set sw [winfo screenwidth .auditconfigwindow]
    set sh [winfo screenheight .auditconfigwindow]
    set ww [winfo width .auditconfigwindow]
    set wh [winfo height .auditconfigwindow]

    set x [expr {($sw - $ww) / 2}]
    set y [expr {($sh - $wh) / 2}]

    wm geometry .auditconfigwindow +${x}+${y}
    wm deiconify .auditconfigwindow 

    raise .auditconfigwindow
    focus -force [tk_focusNext .auditconfigwindow]
}

proc audit::showConfigWindowPopfile {var} {
    upvar $var v
    set dir [file dirname $v]
    set tail [file tail $v]
    set n [tk_getOpenFile -parent .auditconfigwindow -title [msgcat::mc "Select file"] \
	-initialdir $dir -initialfile $tail]
    if {$n == ""} {
	return
    }
    set v [file nativename $n]
}

proc audit::showConfigWindowLevelSet {v} {
    set ::audit::configWindow(levelstring) [lindex $::audit::auditLevels $v]
}

# configuration

proc audit::showConfigWindowCget {} {
    variable configWindow
    set configWindow(level) [audit::cget -level]
    set configWindow(logfile) [audit::cget -logfile]
    set configWindow(datafile) [audit::cget -datafile]
    set configWindow(limit) [audit::cget -limit]

    showConfigWindowLevelSet $configWindow(level)
}

proc audit::showConfigWindowConfigure {} {
    variable configWindow
    audit::configure -level $configWindow(level)
    audit::configure -logfile $configWindow(logfile)
    audit::configure -datafile $configWindow(datafile)
    audit::configure -limit $configWindow(limit)
}

package provide audit::configgui 1.0
